import React, { Component } from 'react';
import CustomBreadcrumb from '../../components/CustomBreadcrumb';
import AddUserForm from './components/AddUserForm';
import { Loading } from '../commons'
import { hashHistory } from 'react-router';
import { UserService, ChannelService } from '../../services'

export default class AddUser extends Component {
  static displayName = 'AddUser';

  constructor(props) {
    super(props);
    this.state = {
      loading : true,
      channel : [],
    };
  }

  componentDidMount = async() =>{
    let channel = await ChannelService.getChannelWithEnable()
    this.setState({ channel: channel.map(c=>{return { label : c.name, value : c.id}}) , loading : false })
  }

  onSubmit = async(data) =>{
    await UserService.save(data)
    hashHistory.push('/user/list')
    return true
  }

  render() {
    const breadcrumb = [
      { text: '用户管理', link: '#/user/list' },
      { text: '新增用户', link: '#/user/add' },
    ];
    return (
      <div className="adduser-page">
        <Loading loading={this.state.loading}/>
        <CustomBreadcrumb dataSource={breadcrumb} />
        { this.state.loading ? null : <AddUserForm channel={this.state.channel} onSubmit={this.onSubmit}/> }
      </div>
    );
  }
}
